package com.mycompany.app.webapp.action.person;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;

import com.mycompany.app.manager.PersonManager;
import com.mycompany.app.model.Person;

public abstract class BaseAction extends org.appfuse.webapp.action.BaseAction {

	private static final PersonManager personManagerStub = new PersonManager(){
		
		private List<Person> persons = new ArrayList<Person>();
		private Long NEXT_ID = 0L;
		
		{
			Person person1 = new Person();
			person1.setFirstName("Franz Allan");
			person1.setLastName("See");
			
			Person person2 = new Person();
			person2.setFirstName("Richard Jayson");
			person2.setLastName("See");
			
			Person person3 = new Person();
			person3.setFirstName("Paul Cyrusse");
			person3.setLastName("See");
			
			save(person1);
			save(person2);
			save(person3);
		}

		public List<Person> getAll() {
			return persons;
		}

		public Person get(Long id) {
			Person matchingPerson = null;
			
			if (persons.isEmpty() == false) {
				Person person;
				Iterator<Person> personIteraor = persons.iterator();
				do {
					person = personIteraor.next();
					if (ObjectUtils.equals(id, person.getId())) {
						matchingPerson = person;
					}
				} while (personIteraor.hasNext() && matchingPerson == null);
			}
			return matchingPerson;
		}
		
		public Person save(Person person) {
			person.setId(NEXT_ID++);
			persons.add(person);
			
			return person;
		}

	};
	protected PersonManager personManager = personManagerStub;

	public void setPersonManager(PersonManager personManager) {
		this.personManager = personManager;
		
	}

}